package cn.kingyang.yiyi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;

import butterknife.ButterKnife;
import cn.kingyang.yiyi.mvp.presenter.BasePresenter;
import cn.kingyang.yiyi.mvp.view.BaseView;
import me.yokeyword.fragmentation.SupportActivity;

/////////////////////////////////////////////////////////////////
//                          _ooOoo_                            //
//                         o8888888o                           //
//                         88" . "88                           //
//                         (| ^_^ |)                           //
//                         O\  =  /O                           //
//                      ____/`---'\____                        //
//                    .'  \\|     |//  `.                      //
//                   /  \\|||  :  |||//  \                     //
//                  /  _||||| -:- |||||-  \                    //
//                  |   | \\\  -  /// |   |                    //
//                  | \_|  ''\---/''  |   |                    //
//                  \  .-\__  `-`  ___/-. /                    //
//                ___`. .'  /--.--\  `. . ___                  //
//              ."" '<  `.___\_<|>_/___.'  >'"".               //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |              //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /              //
//      ========`-.____`-.___\_____/___.-`____.-'========      //
//                           `=---='                           //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      //
//    Code is far away from bug with the buddha protecting!    //
/////////////////////////////////////////////////////////////////

/**
 * Created by KingYang on 2017/2/26.
 * E-Mail: admin@kingyang.cn
 */
public abstract class BaseActivity<V extends BaseView, P extends BasePresenter> extends SupportActivity {
    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        mPresenter = getPresenter();//创建Presenter
        mPresenter.attachView((V) this);

    }

    protected void go(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    protected void start(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    protected void setInputError(TextInputLayout layout, String error) {
        layout.setErrorEnabled(true);
        layout.setError(error);
    }

    protected void clearInputError(TextInputLayout... layout) {
        for (TextInputLayout inputLayout : layout) {
            inputLayout.setError(null);
            inputLayout.setErrorEnabled(false);
        }
    }

    protected String getInputValue(TextInputLayout layout) {
        if (layout.getEditText() != null) {
            return layout.getEditText().getText().toString();
        }
        return "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    protected abstract P getPresenter();

    protected abstract int getLayout();
}
